1978D - Elections - CodeForces Solution


data structures greedy implementation

Please click on ads to support us..

Python Code:

t = int(input())
for tc in range (t):
    n,c = map(int,input().split(' '))
    a = list(map(int,input().split(' ')))
    maxvote = a[0]
    index = 0
    for i in range (0,n):
        if (a[i] > maxvote):
            maxvote = a[i]
            index = i
    if (index == 0):
        maxvote += c
    else:
        if (a[0] + c >= maxvote):
            maxvote = a[0]+c
            index = 0
        preficSum = []
    sum = 0
    for i in range (0,n):
        sum += a[i]
        preficSum.append(sum)

    l = []
    for i in range (0,n):
        if (i== 0):
            if (a[i] + c >= maxvote):
                l.append(0)
            else:
                l.append(1)
        else:
            if (i == index):
                l.append(0)
            elif (a[i] < maxvote):
                if (preficSum[i] + c >= maxvote):
                    l.append(i)
                else:
                    l.append(i+1)
            else:
                if (i <= index):
                    l.append(0)
                else:
                    l.append(i)
    print(' '.join(map(str,l)))


Comments

Submit
0 Comments
More Questions

1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon